import pygame
import os, random

#surface 'decorator'
#pygame.Surface((width, height), flags=0, depth=0, masks=None): return Surface
#pygame.Surface((width, height), flags=0, Surface): return Surface

class SpriteSheetSurface():
    #def __init__(self, img, size, coords_map, max_framenum, replace_colours=None, colorkey_coord=(1,1)):
    def __init__(self, info, team_colour, sleeve_colour, shorts_colour, bgcolour):
        self.__counter=0
        self.__bg_colour= bgcolour
        surface= pygame.image.load(info['path']).convert()
        size = info['size']
        self.__size = size
        coords_map={}
        i=0
        for pos in info['x-positions']:
            coords_map[pos] = [ (i*size[0],0), (i*size[0],size[1]), (i*size[0],size[1]*2) ]
            i+=1
        coords_map['default']= [ (0,size[0]) ]
        
        self.__coords_map = coords_map
        self.__max_framenum = 3
        self.__colorkey_coord= (1,1)
        surface= self.replace_colour(surface, info['sprite_skin_colour'], self.randomish_skin_colour())
        surface= self.replace_colour(surface, info['sprite_sleeve_colour'], sleeve_colour)
        surface= self.replace_colour(surface, info['sprite_shorts_colour'], shorts_colour)
        surface= self.replace_colour(surface, info['sprite_torso_colour'], team_colour)
        
        if self.__colorkey_coord is not None:
            colorkey = surface.get_at(self.__colorkey_coord)
            #print "colorkey: ",colorkey
            self.__colorkey= colorkey
            surface.set_colorkey(colorkey, pygame.RLEACCEL)
        else:
            self.__colorkey= None
            
        self.__sheet = surface

    def randomish_skin_colour(self):
        i= random.randint(0,3)
        if i==0:
            #80 33 00
            return (129,71,30)
        elif i==1:
            #D4 55 00
            return (212,115,85)
        else:
            #FF BB 80
            return (255,153,128)
        
    def replace_sheet_colour(self,colorkey,replacement):
        self.__sheet = self.replace_colour(self.__sheet,colorkey,replacement)
        
    def replace_colour(self, img,colorkey,replacement):
        #colorkey = img.get_at(colorkey_coord)
        #print "colorkey ",colorkey
        img.set_colorkey(colorkey)
        base= img.copy()
        base.fill(replacement)
        base.blit(img,(0,0))
        return base.convert()
        
    def get_size(self):
        return self.__size
    def get_bg_colour(self):
        return self.__bg_colour
    def get_img_at(self, coords):
        rect = pygame.Rect(coords,self.__size)
        subsurface= self.__sheet.subsurface(rect).copy()
        if self.__colorkey is not None:
            subsurface.set_colorkey(self.__colorkey, pygame.RLEACCEL)
        #subsurface.unlock()
        return subsurface
        #image = pygame.Surface(rect.size).convert()
        #image.blit(self.__sheet, (0, 0), rect)
        #return image
        
    #Surface.blit(source, dest, area=None, special_flags = 0): return Rect
    #def blit(self, fg, coords, area=None, special_flags=0):
    #    return self.get_default_img().blit(fg,coords,area, special_flags)
        
    def get_img_by_ref(self, ref, framenum):
        coords= self.__coords_map[ref]
        if framenum is None or len(coords)==1:
            return self.get_img_at(coords[0])
        elif len(coords) > 1:
            #idx= int(len(coords) * framenum/self.__max_framenum)
            return self.get_img_at(coords[framenum])
        else:
            return None
    def get_default_img(self):
        return self.get_img_by_ref('default',0)
        #return self.__sheet.copy()
        
    def get_framenum(self,velocity):
        if(velocity > 0): # or True):
            self.__counter+=1
            if self.__counter % 12 < 4:
                return 0
            elif self.__counter % 12 < 8:
                return 1
            else:
                return 2
        else:
            return 1 #middle one !! TODO
    def get_direction_by_angle(self,angle):
        if angle < 22 or angle >= 270+45+22:
            return 'right'
        if angle < 45+22:
            return 'downright'
        elif angle < 90+22:
            return 'down'
        elif angle < 90+45+22:
            return 'downleft'
        elif angle < 180+22:
            return 'left'
        elif angle < 180+45+22:
            return 'upleft'
        elif angle < 270+22:
            return 'up'
        elif angle < 270+45+22:
            return 'upright'
        else:
            return None
    
    def get_img(self,angle,velocity):
        framenum= self.get_framenum(velocity)
        return self.get_img_by_ref(self.get_direction_by_angle(angle),framenum)
